#pragma once
#include <iostream>
#include <string>
#include <time.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <memory>
#include <unordered_map>

class Session
{
public:
    Session(const std::string& name, const std::string status)
        :_userName(name)
        ,_status(status)
    {
        _createTime = time(nullptr);
    }
public:
    std::string _userName; //用户名
    time_t _createTime;
    std::string _passwd;   //密码
    std::string _status;   //状态
};

using session_sptr = std::shared_ptr<Session>;

class SessionManager
{
public:
    SessionManager()
    {
        srand(time(nullptr));
    }
    std::string AddSession(session_sptr s)
    {
        uint32_t randID = rand() + time(nullptr) ^ getpid();
        std::string sessionID = std::to_string(randID);
        _sessions[sessionID] = s;
        return sessionID;
    }
    session_sptr GetSession(const std::string sessionID)
    {
        if(_sessions.count(sessionID))
            return _sessions[sessionID];
        return nullptr;
    }
private:
    std::unordered_map<std::string, session_sptr> _sessions;
};